package com.mall.web.filter;

import java.io.IOException;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.mall.client.user.SessService;
import com.mall.client.user.UserService;
import com.mall.constant.ReqAttributeKey;
import com.mall.constant.TypeCommOnlyKeyConstant;
import com.mall.pojo.Sess;
import com.mall.pojo.User;
import com.mall.web.util.CookieUtil;
import com.mall.web.util.HttpUrlTypeUtil;
import com.mall.web.util.HttpUtil;
import com.mall.web.util.WebUtil;

public class UserMessageFilter implements Filter {

	@Autowired
	private SessService sessService;
	@Autowired
	private UserService userService;

	@Override
	public void destroy() {

	}

	/**
	 * commononlykey的处理
	 * @param request
	 * @param response
	 */
	void commonOnlyKey(HttpServletRequest request, HttpServletResponse response) {
		if (HttpUrlTypeUtil.isEshopUrl(request)) {
			WebUtil.setCurrentCommOnlyKeySessionAndCookies(request, response, TypeCommOnlyKeyConstant.TYPE_MAIGEDIAN);
		}else if (HttpUrlTypeUtil.isWeiShang(request)) {
			WebUtil.setCurrentCommOnlyKeySessionAndCookies(request, response, TypeCommOnlyKeyConstant.TYPE_WEISHANGB2B);
		}else {
			HttpUtil.getCommkeyId(request, response);
		}
	}

	@Override
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain arg2)
			throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) servletRequest;
		HttpServletResponse response = (HttpServletResponse) servletResponse;
		commonOnlyKey(request, response);
		Cookie[] cookies = request.getCookies();// 这样便可以获取一个cookie数组
		if (cookies == null || cookies.length <= 0) {
			arg2.doFilter(request, response);
			return;
		}
		String cookieValue = CookieUtil.getCookieValue(request, ReqAttributeKey.COOKIE_FOR_USER);

		if (StringUtils.isBlank(cookieValue)) {
			arg2.doFilter(request, response);
			return;
		}
		String commonOnlyKey = CookieUtil.getCookieValue(request, ReqAttributeKey.COOKIE_FOR_COMMKEYID);
		if (StringUtils.isBlank(commonOnlyKey)) {
			arg2.doFilter(request, response);
			return;
		}

		List<Sess> listSess = sessService.getListBySessionKey(cookieValue, Integer.parseInt(commonOnlyKey));
		if (listSess == null || listSess.isEmpty()) {
			arg2.doFilter(request, response);
			return;
		}

		User user = this.userService.getUserByUserId(listSess.get(0).getUid());
		if (user == null) {
			arg2.doFilter(request, response);
			return;
		}
		WebUtil.setCurrentUserSessionAndCookies(request, response, user);
		arg2.doFilter(request, response);

	}

	@Override
	public void init(FilterConfig config) throws ServletException {
		
	}

}
